<?php
	header("Content-type: text/html; charset=utf-8");
	
	$file = 20130715;
	$table = 't_gequ';//故事；

	if($_GET['type']=='move')
	{
		$data = $_->source->_select('*',array('id >'=>10825));//6704
		foreach($data as $val)
		{
			/*
			if(file_exists('station/'.$val['swffile']))
			{
				copy('station/'.$val['swffile'],$file.'/swf/'.$val['swffile']);
			}
			if(file_exists('station/'.$val['imgfile']))
			{
				copy('station/'.$val['imgfile'],$file.'/img/'.$val['imgfile']);
			}
			$str .= 'insert into '.$table.' (Fname,Furl,Fimg,Fflash,Fcode,Fdesc,Fswffile,Fimgfile) values ("'.$val['name'].'","'.$val['link'].'","'.$val['img'].'","'.$val['swf'].'","'.$val['descrp'].'","动画片-史酷比系列","130619/'.$val['swffile'].'","130619/'.$val['imgfile'].'");'."\r\n ";
*/				
			if(file_exists('station/'.$val['swffile']))
			{
				if(copy('station/'.$val['swffile'],$file.'/mp3/'.$val['swffile']))
				{
					$str .= 'insert into t_smp3_info(Ftitle,Fmp3_link,Ftiny_class,Ftype,Fmedia_type,Fbook_type,Fadd_time) values("'.$val['name'].'","http://media.youban.com/gsmp3/mv'.date('Ymd').'/'.$val['swffile'].'","mp3-0715",3,1,0,unix_timestamp());'."\r\n ";
				}
			}
			$_->source->_update(array('step'=>0),array('id'=>$val['id']));
		}
		writeFile($file.'.txt',$str,'other','source/'.$file);
	}

if($_GET['download'] == 'all' or $_POST['download'] == 'all')
{
	$data = $_->source->_select(array('id','link','swf','img','imgfile','swffile'),array('step'=>3));
	$name = time();
	foreach($data as $key=>$val)
	{
		$name++;
		$imgDtag = true;$swfDtag = true;
/*
		if(!$val['imgfile'] && $val['img'])
		{
			$imgfile = $name.'.'.end(explode('.',basename($val['img'])));
			if(copy($val['img'],'station/'.$imgfile))
			{
				$_->source->_update(array('imgfile'=>$imgfile),array('id'=>$val['id']));
			}else{
				$imgDtag = false;
			}
		}
		if(!$val['swffile'] && $val['swf'] && end(explode('.',basename($val['swf'])))=="swf")
		{
			$swffile = $name.'.swf';
			if(copy(parseUrl($val['swf']),'station/'.$swffile))
			{
				$_->source->_update(array('swffile'=>$swffile),array('id'=>$val['id']));
			}else{
				$swfDtag = false;
			}
		}
*/
		if(!$val['swffile'] && end(explode('.',basename($val['swf'])))!="swf")
		{
			$url = str_replace('filemp3','au_play',$val['swf']);
			$ch = curl_init();    
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_REFERER, $val['link']);
			curl_setopt($ch, CURLOPT_HEADER, 0);    
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0');
			$content = curl_exec($ch);    
			curl_close($ch);
			preg_match("/mp3:\"([^\"]+)\"/i", $content, $m);
			//print_r($val['swf']);exit;
			$mp3 = parseUrl(trim(iconv("gb2312", "utf-8", $m[1])));
			
			$swffile = $name.'.'.end(explode('.',basename($mp3)));
			if(copy($mp3,'station/'.$swffile))
			{
				$_->source->_update(array('swffile'=>$swffile),array('id'=>$val['id']));
			}else{
				$swfDtag = false;
			}
		}

		if($imgDtag && $swfDtag)
		{
			$_->source->_update(array('step'=>3),array('id'=>$val['id']));
		}else{
			die('Err');
		}
	}
	die('OK');	
}

//61gequ
/*
switch($_POST['step'])
{
	case 1:
		if(intval($_POST['id']) < 1)
		{
			echo json_encode(array('res'=>'Err'));exit;
		}
		$id = intval($_POST['id']);
		$data = $_->source->_select('*',array('id'=>$id));
		$data = $data[0];

		if($swf_file = file_get_contents($data['link']))
		{
			preg_match_all("/swfurl : \"([^\"]+)\"/",$swf_file, $d, PREG_PATTERN_ORDER);
		}
		$link = $d[1][0];

		if($link)
		{
			$_->source->_update(array('code'=>$link,'step'=>2),array('id'=>$id));
			echo json_encode(array('res'=>'OK'));exit;
		}
		echo json_encode(array('res'=>'Err'));exit;
	break;

	case 2:
		$id = intval($_POST['id']);
		if($_->source->_update(array('swf'=>$_POST['swfurl'],'step'=>3),array('id'=>$id)))
		{
			echo json_encode(array('res'=>'OK'));exit;
		}else{
			echo json_encode(array('res'=>'Err'));exit;
		}
	break;
}
*/
if($_POST['fromid'] == 1)//bb88 mp3
{
	switch($_POST['step'])
	{
		case 1:
			$id = intval($_POST['id']);
			if($id < 1)
			{
				echo json_encode(array('res'=>'Err'));exit;
			}
			
			$data = $_->source->_select('*',array('id'=>$id));
			$data = $data[0];
			if($swf_file = file_get_contents($data['link']))
			{
				preg_match_all("/<script type=\"text\/javascript\" language=\"javascript\" src=\"\/au_play.php\?id=(\d+)\"><\/script>/",iconv("gb2312", "utf-8", $swf_file), $d, PREG_PATTERN_ORDER);
			}
			$link = 'http://www.baobao88.com/filemp3.php?id='.trim($d[1][0]);
			if(trim($d[1][0]))
			{
				$_->source->_update(array('swf'=>$link,'step'=>3),array('id'=>$id));
				echo json_encode(array('res'=>'<b>待下载...</b>'));exit;
			}
			echo json_encode(array('res'=>'Err'));exit;
		break;
	}
}
/*
//bb88 swf

switch($_POST['step'])
{
	case 1:
		if(intval($_POST['id']) < 1)
		{
			echo json_encode(array('res'=>'Err'));exit;
		}
		$id = intval($_POST['id']);
		$data = $_->source->_select('*',array('id'=>$id));
		$data = $data[0];
		$domain = 'http://www.baobao88.com';

		if($swf_file = file_get_contents($domain.$data['link']))
		{
			preg_match_all("/showflash\(\"([^\"]+)\",\"flashgame\",670,480\)/",iconv("gb2312", "utf-8", $swf_file), $d, PREG_PATTERN_ORDER);
			//preg_match_all("/<a href=\"([^\"]+)\" target=\"_blank\" class=\"play_btn\">/i",$swf_file, $c, PREG_PATTERN_ORDER);
		}

		$link = $d[1][0];

		if($link)
		{
			$_->source->_update(array('swf'=>$link,'step'=>3),array('id'=>$id));
			echo json_encode(array('res'=>'OK'));exit;
		}
		echo json_encode(array('res'=>'Err'));exit;
	break;

	case 2:
		$id = intval($_POST['id']);
		if($_->source->_update(array('swf'=>$_POST['swfurl'],'step'=>3),array('id'=>$id)))
		{
			echo json_encode(array('res'=>'OK'));exit;
		}else{
			echo json_encode(array('res'=>'Err'));exit;
		}
	break;
}
/*

//61bb
switch($_POST['step'])
{
	case 1:
		if(intval($_POST['id']) < 1)
		{
			echo json_encode(array('res'=>'Err'));exit;
		}
		$id = intval($_POST['id']);
		$data = $_->source->_select('*',array('id'=>$id));
		$data = $data[0];
		$domain = 'http://www.61baobao.com';
		if($swf_file = file_get_contents($data['link']))
		{
			preg_match_all("/<div id=\"nonesay\" style=\"display:none;\">(.+)<\/div>/",iconv("gb2312", "utf-8", $swf_file), $d, PREG_PATTERN_ORDER);
			preg_match_all("/<a href=\"([^\"]+)\" target=\"_blank\" class=\"play_btn\">/i",$swf_file, $c, PREG_PATTERN_ORDER);
		}

		$descrp = $d[1][0];
		$link = $domain.$c[1][0];
		if($c[1][0])
		{
			if($swf_file = file_get_contents($link))
			{
				preg_match_all("/<script>addswf\(\'(.+)\'\);(?:\s+)<\/script>/",iconv("gb2312", "utf-8", $swf_file), $a, PREG_PATTERN_ORDER);
			}
		}	
		if(!$data['code'] && $a[1][0])
		{
			$_->source->_update(array('code'=>$a[1][0],'descrp'=>$descrp,'step'=>2),array('id'=>$id));
		}else{
			echo json_encode(array('res'=>'Err'));exit;
		}
		echo json_encode(array('res'=>'OK','code'=>$a[1][0]));exit;
	break;

	case 2:
		$id = intval($_POST['id']);
		if($_->source->_update(array('swf'=>$_POST['swfurl'],'step'=>3),array('id'=>$id)))
		{
			echo json_encode(array('res'=>'OK'));exit;
		}else{
			echo json_encode(array('res'=>'Err'));exit;
		}
	break;
}
*/
/*
//7k7k
	$domain = 'http://www.7k7k.com';

	if($swf_file = file_get_contents($data['link']))
	{
		preg_match_all("/_gamepath = \"([^\"]+)\",/i",$swf_file, $b, PREG_PATTERN_ORDER);
		preg_match_all("/<h3>游戏介绍：<\/h3>(\s+)<p>(\s+)(.+)(\s+)<\/p>/",$swf_file, $d, PREG_PATTERN_ORDER);
		preg_match_all("/<div class=\"btn_play\"><a href=\"([^\"]+)\"/i",$swf_file, $c, PREG_PATTERN_ORDER);
	}
	
	if(strpos($c[1][0],'http') === false)
	{
		$url = $domain.$c[1][0];
	}else{
		$url = $c[1][0];
	}
	if($c[1][0])
	{
		if($swf_file = file_get_contents($url))
		{
			preg_match_all("/_gamepath = \"([^\"]+)\",/i",$swf_file, $b, PREG_PATTERN_ORDER);
			preg_match_all("/<h3>游戏介绍：<\/h3>(\s+)<p>(\s+)(.+)(\s+)<\/p>/",$swf_file, $d, PREG_PATTERN_ORDER);
		}

		$descrp = strip_tags($d[3][0]);

		if(end(explode('.',basename($b[1][0])))=='swf')
		{
			$swf = $b[1][0];
		}else{
			if($swf_file = file_get_contents($b[1][0]))
			{
				preg_match_all("/p = \"([^\"]+)\";/i",$swf_file, $f, PREG_PATTERN_ORDER);
				preg_match_all("/_src_ \= \'([^\"]+)\';/i",$swf_file, $e, PREG_PATTERN_ORDER);
			}
			
			if($e[1][0])
			{
				$swf = dirname($b[1][0]).'/'.$e[1][0];
				
			}
			if($f[1][0])
			{
				$swf = dirname($b[1][0]).'/'.$f[1][0];
			}
			
		}
	}
	*/

//4399
/*
$step = $_GET['step']?$_GET['step']:$_POST['step'];
switch($step)
{
	case 1:
		if(intval($_POST['id']) < 1)
		{
			echo json_encode(array('res'=>'Err'));exit;
		}
		$id = intval($_POST['id']);

		$datas = $_->source->_select('*',array('id'=>$id));
		$data = $datas[0];

		$domain = 'http://www.4399.com';
		if(strpos($val['url'],'http') === false)
		{
			$url = $domain.$data['link'];
		}else{
			$url = $data['link'];
		}


		if($swf_file = file_get_contents($url))
		{
			preg_match_all("/<script language=\"javascript\" src=\"(\/js\/serve(.+).js)\"><\/script>/i",$swf_file, $a, PREG_PATTERN_ORDER);
			preg_match_all("/_strGamePath=\"([^\"]+)\"/i",$swf_file, $b, PREG_PATTERN_ORDER);
			preg_match_all("/<div class=\"start_game\"><a href=\"([^\"]+)\" target=\"_self\">/i",$swf_file, $c, PREG_PATTERN_ORDER);
			preg_match_all("/<b class=\"strongb\">游戏介绍<\/b>(\s+)<p>(.+)<\/p>/",iconv("gb2312", "utf-8", $swf_file), $d, PREG_PATTERN_ORDER);
		}

		$jsUrl = $domain.$a[1][0];
		if($js_file = file_get_contents($jsUrl))
		{
			preg_match_all("/var webServer = \"([^\"]+)\"/i",$js_file, $e, PREG_PATTERN_ORDER);
		}
		$jsDomain = $e[1][0];
		$jsDir = $b[1][0];
		$flashUrl = $c[1][0];//详细页存在
		
		$descrp = strip_tags($d[2][0]);

		if($flashUrl)
		{//打开详细页
			
			if(strpos($flashUrl,'http') === false)
			{
				$flashUrl = $domain.$flashUrl;
			}
			if($swf_file = file_get_contents($flashUrl))
			{
				preg_match_all("/<script language=\"javascript\" src=\"(\/js\/serve(.+).js)\"><\/script>/i",$swf_file, $a, PREG_PATTERN_ORDER);
				preg_match_all("/_strGamePath=\"([^\"]+)\"/i",$swf_file, $b, PREG_PATTERN_ORDER);
				preg_match_all("/<b class=\"strongb\">游戏介绍<\/b>(\s+)<p>(.+)<\/p>/",iconv("gb2312", "utf-8", $swf_file), $c, PREG_PATTERN_ORDER);
			}

			$descrp = strip_tags($c[2][0]);
			if($js_file = file_get_contents($domain.$a[1][0]))
			{
				preg_match_all("/var webServer = \"([^\"]+)\"/i",$js_file, $d, PREG_PATTERN_ORDER);
			}
			
			if(end(explode('.',basename($b[1][0])))=='swf')
			{
				$swf = $d[1][0].$b[1][0];
			}else{
				if($swf_file = file_get_contents($d[1][0].$b[1][0]))
				{
					preg_match_all("/src=\"([^\"]+swf)\"/i",$swf_file, $f, PREG_PATTERN_ORDER);
					if($f[1][0])
					{
						$swf = dirname($d[1][0].$b[1][0]).'/'.$f[1][0];
					}
				}
			}
		}elseif(end(explode('.',basename($jsDir)))=='swf')
		{
			$swf = $jsDomain.$jsDir;
		}else{
			//打开swf页面
			if($swf_file = file_get_contents($jsDomain.$jsDir))
			{
				preg_match_all("/src=\"([^\"]+swf)\"/i",$swf_file, $f, PREG_PATTERN_ORDER);
				if($f[1][0])
				{
					$swf = dirname($jsDomain.$jsDir).'/'.$f[1][0];
				}
				
			}
		}
		if($swf)
		{
			$_->source->_update(array('swf'=>$swf,'descrp'=>$descrp,'step'=>3),array('id'=>$data['id']));
			echo json_encode(array('res'=>'OK'));exit;
		}
		echo json_encode(array('res'=>'Err'));exit;
	break;

	case 2:
		$id = intval($_POST['id']);
		if($_->source->_update(array('swf'=>$_POST['swfurl'],'step'=>3),array('id'=>$id)))
		{
			echo json_encode(array('res'=>'OK'));exit;
		}else{
			echo json_encode(array('res'=>'Err'));exit;
		}
	break;
	case 'all':
		$datas = $_->source->_select('*',array('step'=>1));

foreach($datas as $data){

		$domain = 'http://www.4399.com';
		if(strpos($val['url'],'http') === false)
		{
			$url = $domain.$data['link'];
		}else{
			$url = $data['link'];
		}

		if($swf_file = file_get_contents($url))
		{
			preg_match_all("/<script language=\"javascript\" src=\"(\/js\/serve(.+).js)\"><\/script>/i",$swf_file, $a, PREG_PATTERN_ORDER);
			preg_match_all("/_strGamePath=\"([^\"]+)\"/i",$swf_file, $b, PREG_PATTERN_ORDER);
			preg_match_all("/<div class=\"start_game\"><a href=\"([^\"]+)\" target=\"_self\">/i",$swf_file, $c, PREG_PATTERN_ORDER);
			preg_match_all("/<b class=\"strongb\">游戏介绍<\/b>(\s+)<p>(.+)<\/p>/",iconv("gb2312", "utf-8", $swf_file), $d, PREG_PATTERN_ORDER);
		}

		$jsUrl = $domain.$a[1][0];
		if($js_file = file_get_contents($jsUrl))
		{
			preg_match_all("/var webServer = \"([^\"]+)\"/i",$js_file, $e, PREG_PATTERN_ORDER);
		}
		$jsDomain = $e[1][0];
		$jsDir = $b[1][0];
		$flashUrl = $c[1][0];//详细页存在
		
		$descrp = strip_tags($d[2][0]);

		if($flashUrl)
		{//打开详细页
			
			if(strpos($flashUrl,'http') === false)
			{
				$flashUrl = $domain.$flashUrl;
			}
			if($swf_file = file_get_contents($flashUrl))
			{
				preg_match_all("/<script language=\"javascript\" src=\"(\/js\/serve(.+).js)\"><\/script>/i",$swf_file, $a, PREG_PATTERN_ORDER);
				preg_match_all("/_strGamePath=\"([^\"]+)\"/i",$swf_file, $b, PREG_PATTERN_ORDER);
				preg_match_all("/<b class=\"strongb\">游戏介绍<\/b>(\s+)<p>(.+)<\/p>/",iconv("gb2312", "utf-8", $swf_file), $c, PREG_PATTERN_ORDER);
			}

			$descrp = strip_tags($c[2][0]);
			if($js_file = file_get_contents($domain.$a[1][0]))
			{
				preg_match_all("/var webServer = \"([^\"]+)\"/i",$js_file, $d, PREG_PATTERN_ORDER);
			}
			
			if(end(explode('.',basename($b[1][0])))=='swf')
			{
				$swf = $d[1][0].$b[1][0];
			}else{
				if($swf_file = file_get_contents($d[1][0].$b[1][0]))
				{
					preg_match_all("/src=\"([^\"]+swf)\"/i",$swf_file, $f, PREG_PATTERN_ORDER);
					if($f[1][0])
					{
						$swf = dirname($d[1][0].$b[1][0]).'/'.$f[1][0];
					}
				}
			}
		}elseif(end(explode('.',basename($jsDir)))=='swf')
		{
			$swf = $jsDomain.$jsDir;
		}else{
			//打开swf页面
			if($swf_file = file_get_contents($jsDomain.$jsDir))
			{
				preg_match_all("/src=\"([^\"]+swf)\"/i",$swf_file, $f, PREG_PATTERN_ORDER);
				if($f[1][0])
				{
					$swf = dirname($jsDomain.$jsDir).'/'.$f[1][0];
				}
				
			}
		}
		if($swf)
		{
			$_->source->_update(array('swf'=>$swf,'descrp'=>$descrp,'step'=>3),array('id'=>$data['id']));
			echo json_encode(array('res'=>'OK')).'<br>';
		}
		echo json_encode(array('res'=>'Err')).'<br>';
}
		break;
}

	*/

//4399
/*
	$domain = 'http://www.3366.com';

	$url = preg_replace('/game/','flash',$domain.$data['link']);
	
	if($swf_file = file_get_contents($url))
	{
		preg_match_all("/\"GameId\":\"(\d+)\",/",$swf_file, $a, PREG_PATTERN_ORDER);
		preg_match_all("/\"GameType\":\"(\d+)\",/",$swf_file, $b, PREG_PATTERN_ORDER);
		preg_match_all("/\"OpenFlashBaseDirExp\": \"(.*)\"/",$swf_file, $c, PREG_PATTERN_ORDER);
		preg_match_all("/<h3>小游戏简介：<\/h3>(\s+)<p>(.+)<\/p>/",iconv("gb2312", "utf-8", $swf_file), $d, PREG_PATTERN_ORDER);
	}

	$gameId = $a[1][0];
	$gameType = $b[1][0];
	$openFlashBaseDirExp = $c[1][0];
	
	$Fdomain = 'http://img.3366img.com';
	if($gameType == 2)
	{
		$Fdomain = 'http://img.3366.com';
	}
	
	$swf = $Fdomain.'/fileupload/flash/'.($gameId%100).'/'.$gameId.'.swf';
	$descrp = strip_tags($d[2][0]);
	
	$res = $_->source->_update(array('swf'=>$swf,'descrp'=>$descrp),array('id'=>$id));

	if($swf && $res)
	{
		die('OK');
	}else{
		die('Err');
	}


//2144
$domain = "http://www.2144.cn";
switch($_POST['step'])
{
	case 1:
		$data = $_->source->_select(array('id','link'),array('step'=>1));
		foreach($data as $key=>$val)
		{
			
			$h = end(explode('.',basename($val['link'])));
			if(in_array($h, array('shtml','html','php','htm')))
			{
				if($swf_file = iconv("gb2312", "utf-8", file_get_contents($domain.$val['link'])))
				{
					preg_match_all("/<a href=\"([^\"]+)\" class=\"detail_playbtn\"/i",$swf_file, $a, PREG_PATTERN_ORDER);				
				}			
				$val['link'] = $a[1][0];
			}
			
			if($swf_file = file_get_contents($domain.$val['link']))
			{
				preg_match_all("/game_filename=\'([^\']+)\'/i",$swf_file, $swf, PREG_PATTERN_ORDER);//swf 链接	
				preg_match_all("/<h3 id=(?:[^>]*)>([^h]+)<\/h3>/i",$swf_file, $b, PREG_PATTERN_ORDER);
				preg_match_all("/<div id=\"g_introduce\">(.+)<\/div>/i",$swf_file, $c, PREG_PATTERN_ORDER);
				$dlist = $b[1]?array_merge($b[1],$c[1]):array_merge($c[1],$b[1]);
				if($dlist):
				foreach($dlist as &$v)
				{
					$v = iconv("gb2312", "utf-8", trim(strip_tags($v)));
				}
				endif;
				$desc = implode('|',$dlist);
			}else{
				die('ERR');
			}

			if($swf = trim($swf[1][0]))
			{
				
				if(strpos($swf,'http') === false)
				{
					$swf = "http://flash.2144.cn/qigongzhu/".$swf;
				}
				$res = $_->source->_update(array('swf'=>$swf,'descrp'=>$desc,'step'=>2),array('id'=>$val['id']));
				if(!$res)
				{
					die('ERR');
				}
			}else{
				die('ERR');
			}
		}
		break;
	case 2:
		break;
}
*/
?>